﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Threading;

namespace DirectoryMonitor
{
    public partial class Form1 : Form
    {
        private bool windowMaximized = false;
        private Form fullScreenForm = new Form();
        private PictureBox pictureBox2 = new PictureBox();
                    
        public Form1()
        {
            InitializeComponent();
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                pathBox.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                toolStripStatusLabel1.Text = "Monitoring already started";
                statusStrip1.Refresh();
            }
            else
            {
                toolStripStatusLabel1.Text = "Monitor running";
                statusStrip1.Refresh();
                StartThread();
            }
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                toolStripStatusLabel1.Text = "Waiting to stop monitoring...";
                statusStrip1.Refresh();
            }
            if (!backgroundWorker1.CancellationPending)
            {
                // Cancel async worker
                this.backgroundWorker1.CancelAsync();
            }

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //MessageBox.Show("Error: " + e.Error.Message);
                toolStripStatusLabel1.Text = "Error: " + e.Error.Message;
                statusStrip1.Refresh();
            }
            else if (e.Cancelled)
            {
                toolStripStatusLabel1.Text = "Monitoring stopped";
                statusStrip1.Refresh();
            }
            else
            {
                toolStripStatusLabel1.Text = "Finished";
                statusStrip1.Refresh();
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            monitorPath.CurrentState state = (monitorPath.CurrentState)e.UserState;
            debugList.Items.Add(state.mostRecentFile);
            pictureBox1.Load(state.mostRecentFile);
            if (windowMaximized)
            {
                //Form fullScreenForm = this.Owner;
                pictureBox2.Load(state.mostRecentFile);
            }
            else
            {
                pictureBox1.Load(state.mostRecentFile);

            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker;
            worker = (BackgroundWorker)sender;
            monitorPath mp = (monitorPath)e.Argument;
            mp.checkForNewFiles(worker,e);
        }
        private void StartThread()
        {
            //This method runs on the main thread
            monitorPath mp = new monitorPath();
            mp.pathToMonitor = pathBox.Text;
            mp.monitorInterval = monitorInterval.Value;
            // Start the asynchronus operation
            backgroundWorker1.RunWorkerAsync(mp);
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            // Toggle full screen mode by pressing F11
            if (e.KeyData == Keys.F11)
            {
                if (!windowMaximized)
                {
                    windowMaximized = true;

                    //Form fullScreenForm = new Form();
                    this.Owner = fullScreenForm;
                    this.Visible = false;

                    fullScreenForm.ControlBox = false;
                    fullScreenForm.TopMost = true;
                    fullScreenForm.WindowState = FormWindowState.Maximized;
                    //f.FormBorderStyle = FormBorderStyle.None;
                    //f.StartPosition = FormStartPosition.Manual;
                    //f.Location = new Point(100,100);

                    //PictureBox pictureBox2 = new PictureBox();
                    pictureBox2.Dock = DockStyle.Fill;
                    pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                    fullScreenForm.Controls.Add(pictureBox2);

                    if (File.Exists(pictureBox1.ImageLocation))
                    {
                        pictureBox2.Image = Image.FromFile(pictureBox1.ImageLocation);
                    }




                    fullScreenForm.Show();
                    fullScreenForm.KeyUp += new KeyEventHandler(Form1_KeyUp);

                }
                else
                {
                    windowMaximized = false;


                    Form fullScreenForm = this.Owner;
                    this.Owner = null;
                    this.Visible = true;
                    //fullScreenForm.Close();
                    fullScreenForm.Visible = false;



                }
            }
        }


    }
}
